package cn.hfatec.shms.web.xc.xcAkkdpm.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.HashMap;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.web.xc.xcAkkdpm.model.XcAkkdpm;
import cn.hfatec.shms.web.xc.xcAkkdpm.dao.XcAkkdpmDao;
import cn.hfatec.shms.web.xc.xcAkkdpm.model.XcAkkdpmParams;

import javax.annotation.Resource;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("XcAkkdpmService")
public class XcAkkdpmService {

    public static final String BEAN_ID = "XcAkkdpmService";

    //注入Dao层
    @Resource(name = "XcAkkdpmDao")
    private XcAkkdpmDao xcAkkdpmDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     * 查询数据
     *
     * @param xcAkkdpmParams
     * @param page
     */
    @Transactional
    public Table queryXcAkkdpmTable(XcAkkdpmParams xcAkkdpmParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        String bzStart = null;
        if ((xcAkkdpmParams.getTjStart() == null) || (xcAkkdpmParams.getTjStart().isEmpty())) {
            bzStart = getBzstart();
            xcAkkdpmParams.setTjStart(bzStart);
        } else {
            bzStart = xcAkkdpmParams.getTjStart();
        }
        if ((xcAkkdpmParams.getTjEnd() == null) || (xcAkkdpmParams.getTjEnd().isEmpty())) {
            String riqiend = getBzend(bzStart);
            xcAkkdpmParams.setTjEnd(riqiend);
        }
        params.put("params", xcAkkdpmParams);
        return baseDao.queryTableByPage("queryXcAkkdpm", params, page);
    }

    public String getBzstart() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.add(Calendar.DATE, -1);
        String bzstart = df.format(cal.getTime());
        return bzstart;
    }

    public String getBzend(String bzStart) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date myDate = null;
        String bzend = null;
        try {
            myDate = formatter.parse(bzStart);
            Calendar c = Calendar.getInstance();
            c.setTime(myDate);
            c.add(Calendar.DATE, 6);
            myDate = c.getTime();
            bzend = formatter.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bzend;
    }

    /**
     * 插入数据
     *
     * @param xcAkkdpm
     */
    @Transactional
    public void saveXcAkkdpm(XcAkkdpm xcAkkdpm) {
        if (xcAkkdpm.getId() != null) {
            XcAkkdpm result = getXcAkkdpm(xcAkkdpm.getId());
            if (result != null) {
                xcAkkdpmDao.update(xcAkkdpm);
            } else {
                xcAkkdpmDao.insert(xcAkkdpm);
            }
        } else {
            xcAkkdpmDao.insert(xcAkkdpm);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    public void deleteXcAkkdpm(Integer id) {

        xcAkkdpmDao.delete(id);
    }

    /**
     * 删除数据
     *
     * @param xcAkkdpmList
     */
    @Transactional
    public void deleteXcAkkdpmBatch(List<String> xcAkkdpmList) {
        for (String id : xcAkkdpmList) {
            deleteXcAkkdpm(Integer.valueOf(id));
        }
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    public XcAkkdpm getXcAkkdpm(Integer id) {

        return xcAkkdpmDao.getXcAkkdpmById(id);
    }
}